home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
TeX 1995 July
/
TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO
/
web
/
noweb
/
src
/
c
/
markup.c
< prev
next >
Wrap
C/C++ Source or Header
|
1995-02-24
|
4KB
|
147 lines
#line 24 "markup.nw"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <assert.h>
#include "markup.h"
#include "strsave.h"
#include "errors.h"
#line 60 "markup.nw"
char at_sign = '@'; /* should be the only place '@' is mentioned */
#line 85 "markup.nw"
static char def_marker[] = " %def ";
#define def_length (6)
#line 71 "markup.nw"
int starts_doc(char *line) {
return (*line==at_sign && (line[1]=='\0' || isspace(line[1])));
}
char *first_doc_line(char *line) {
if (line[1]!='\0' && line[1] !='\n') return line+2;
else return line+1;
}
#line 88 "markup.nw"
int is_def(char *line) {
#line 97 "markup.nw"
{ static int checked = 0;
if (!checked) {
assert(strlen(def_marker) == def_length);
checked = 1;
}
}
#line 90 "markup.nw"
return (*line==at_sign && !strncmp(line+1, def_marker, def_length));
}
char *remove_def_marker(char *line) {
return line+1+def_length;
}
#line 126 "markup.nw"
char *mod_start(char *s, int mark) {
return find_escaped(s,"<<","@<<", mark);
}
char *mod_end(char *s, int mark) {
return find_escaped(s,">>","@>>", mark);
}
#line 141 "markup.nw"
int starts_code (char *line, char *filename, int lineno) {
char *tail;
if (mod_start(line,0) != line+2) return 0;
tail = mod_end(line+2,0);
if (tail == NULL)
#line 164 "markup.nw"
{
errorat(filename, lineno, Error, "Module name doesn't end", line);
return 0;
}
#line 146 "markup.nw"
if (*tail++ != '=') return 0;
while (isspace(*tail)) tail++;
return (*tail == '\0');
}
void getmodname(char *dest, int size, char *source) {
/* excess characters in the module name are ignored */
char *p = strsave(source);
char *q = mod_start(p,1);
if (q==NULL)
#line 169 "markup.nw"
{
free(p);
impossible
("I couldn't manage to extract a module name, but I'm sure I saw one");
}
#line 157 "markup.nw"
if (mod_end(q,1)==NULL)
#line 169 "markup.nw"
{
free(p);
impossible
("I couldn't manage to extract a module name, but I'm sure I saw one");
}
#line 158 "markup.nw"
strncpy(dest,q,size-1);
dest[size-1] = '\0';
free(p);
}
#line 181 "markup.nw"
char *quote_start(char *s, int mark) {
return find_escaped(s,"[[",NULL, mark);
}
#line 188 "markup.nw"
char *quote_end(char *s, int mark) {
char *t = find_escaped(s, "]]", NULL, 0);
if (t == NULL)
return t;
else {
t -= 2; /* subtract length of ]] */
assert(t[0] == ']' && t[1] == ']');
while (t[2] == ']') t++;
assert(t[0] == ']' && t[1] == ']');
if (mark) *t = 0;
return t+2;
}
}
#line 220 "markup.nw"
char *find_escaped(register char *s, char *search, char *escape, int mark) {
register char first = *search;
register char first_escape = (escape != NULL ? *escape : '\0');
int searchlen = strlen(search);
int escapelen = (escape != NULL ? strlen(escape) : 0);
do {
while (*s && *s != first && *s != first_escape) s++;
if (!*s) break;
if (first_escape && !strncmp(s,escape,escapelen)) {
s += escapelen;
continue;
}
if (!strncmp(s,search,searchlen)) break;
s++;
} while (*s != '\0');
/* here either s is empty or it points to the first unescaped [[search]] */
if (*s == '\0') return NULL;
if (mark) *s = '\0';
return s+searchlen;
}
#line 253 "markup.nw"
char *unescape (char *s) {
char *t = malloc(strlen(s)+1);
char *r = t;
checkptr(t);
while (*s != '\0') {
if (*s == at_sign) {
if (!strncmp(s+1,"<<",2) || !strncmp(s+1,">>",2)) s++;
}
*t++ = *s++;
}
*t = '\0';
return r;
}